package app.utils;

import app.auth.AccessManager;
import app.vo.User;
import io.javalin.http.Context;

import java.util.HashMap;
import java.util.Map;

/**
 * A Utility Class with useful methods for Views of our application
 *
 * @author Sebastian Rodriguez, 2020. email: sebastian.rodriguez@rmit.edu.au
 */
public class Views {

    /**
     * Use this method to populate the model with common information.
     * Eg: model.put("user", getSessionCurrentUser(ctx));
     * @param ctx
     * @return a pre-populated model
     */
    public static Map<String, Object> baseModel(Context ctx) {
        Map<String, Object> model = new HashMap<>();
        User user = AccessManager.getSessionCurrentUser(ctx);
        if (null == user || user.getId().equals("00")){
            user = new User("00", "consumer", "consumer");
            AccessManager.loginUser(ctx, user);
            model.put("user_logged_in", false);
        } else {
            model.put("user_logged_in", true);
        }
        model.put("user", user);
        return model;
    }

}
